package cn.yunhe.video.controller;

import cn.yunhe.video.Service.IGreatService;
import cn.yunhe.video.Service.IVideoService;
import cn.yunhe.video.entity.Great;
import cn.yunhe.video.entity.UserInfo;
import cn.yunhe.video.entity.Video;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
public class GreatController {
    @Autowired
    private IGreatService greatService;

    @Autowired
    private IVideoService iVideoService;

    @RequestMapping (value = "/addLike")
    public void addLike(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id =  Integer.parseInt(request.getParameter("id"));
        int uid = Integer.parseInt(request.getParameter("uid"));
        Video video = new Video();
        video.setId(id);
        UserInfo userInfo = new UserInfo();
        userInfo.setUid(uid);
        Great great = new Great();
        great.setUserInfo(userInfo);
        great.setVideo(video);
        boolean flag = iVideoService.addLike(id);
        boolean flag1= greatService.addLike(great);
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        if(flag&&flag1){
            out.print("true");
        }
    }
    @RequestMapping (value = "/delLike")
    public void delLike(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println(request.getParameter("id"));
        int id =  Integer.parseInt(request.getParameter("id"));
        int uid = Integer.parseInt(request.getParameter("uid"));
        Video video = new Video();
        video.setId(id);
        UserInfo userInfo = new UserInfo();
        userInfo.setUid(uid);
        Great great = new Great();
        great.setUserInfo(userInfo);
        great.setVideo(video);
        boolean flag = iVideoService.delLike(id);
        boolean flag1= greatService.delLike(great);
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        if(flag&&flag1){
            out.print("true");
        }
    }

}
